const LETTERS = /[a-z]/i;
const WHITESPACE = /\s/;
const NUMBERS = /[0-9]/;

const LeftParentheses = 'LeftParentheses'; // 左括号 <
const JSXIdentifier = 'JSXIdentifier';

let currentToken = {type: '', value: ''};

const tokens = [];
function emit(token) {
    currentToken = {type: '', value: ''};
    tokens.push(token);
}


function start (char) {
    if (char === '<') {
        emit({type: LeftParentheses, value: '<'});
        return foundLeftParentheses;
    }
    throw new Error('第一个字符必须是 < ');
}
function eof() {
    if (currentToken.value.length > 0) {
        emit(currentToken);
    }
}

function foundLeftParentheses(char) {
    if (LETTERS.test(char)) { // 小写字母
        currentToken.type = JSXIdentifier;
        currentToken.value += char;
        return jsxIdentifier; // 继续收集标识符
    }
}

function jsxIdentifier(char) {
    if (NUMBERS.test(char)) { // 数字
        currentToken.value += char;
        return jsxIdentifier;
    } else if (WHITESPACE.test(char)) { // 空格，表示标识符结束
        emit(currentToken);
        return eof();
    }
    return eof;
}

function tokenizer (input) {
    let state = start; // 开始处于的状态

    for (const char of input) {
        if (state) {
            state = state(char);
        }
    }

    return tokens;
}

module.exports = {
    tokenizer
}

const sourceCode = '<h1 id="title"><span>hello</span>world</h1>'
console.log(tokenizer(sourceCode));
/**
[
    { type: 'LeftParentheses', value: '<' },
    { type: 'JSXIdentifier', value: 'h1' }
]
 */